package com.speedata;

import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.serialport.DeviceControlSpd;
import android.serialport.SerialPortSpd;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.speedata.libutils.DataConversionUtils;
import com.speedata.uhftest.R;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author xuyan   祥林调试打印20190422
 */
public class PrintActivity extends AppCompatActivity implements View.OnClickListener {

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;

    private DeviceControlSpd deviceControl;
    private SerialPortSpd serialPort = null;
    private int fd = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_print);
        initView();
        initDevice();
        initSerial();
    }

    private void initDevice() {
        try {
            deviceControl = new DeviceControlSpd(DeviceControlSpd.POWER_MAIN);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initSerial() {
        try {
            serialPort = new SerialPortSpd();
            serialPort.OpenSerial("/dev/ttyMT3", 115200);
            fd = serialPort.getFd();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        bt1 = findViewById(R.id.bt1);
        bt2 = findViewById(R.id.bt2);
        bt3 = findViewById(R.id.bt3);
        bt4 = findViewById(R.id.bt4);
        bt5 = findViewById(R.id.bt5);
        bt6 = findViewById(R.id.bt6);
        bt7 = findViewById(R.id.bt7);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                try {
                    deviceControl.MainPowerOn(85);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt2:
                try {
                    deviceControl.MainPowerOff(85);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.bt3:
                open("0on");
                Toast.makeText(this, "0on", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt4:
                open("0off");
                Toast.makeText(this, "0off", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt5:
                if (fd != -1) {
                    serialPort.WriteSerialByte(fd, DataConversionUtils.HexString2Bytes("1B061B63001B16"));
                    Toast.makeText(this, "1B061B63001B16", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt6:
                if (fd != -1) {
                    serialPort.WriteSerialByte(fd, DataConversionUtils.HexString2Bytes("1B061B63011B16"));
                    Toast.makeText(this, "1B061B63011B16", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt7:
                //重启
                PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
                pm.reboot("");
                break;
            default:
                break;
        }
    }


    private static BufferedWriter TorchFileWrite;

    /**
     * 9523
     *
     * @param str
     */
    public static void open(String str) {
        File TorchFileName = new File("/sys/class/misc/aw9523/gpio");
        try {
            TorchFileWrite = new BufferedWriter(new FileWriter(TorchFileName, false));
            TorchFileWrite.write(str);
            TorchFileWrite.flush();
            TorchFileWrite.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        if (serialPort != null) {
            serialPort.CloseSerial(fd);
        }
        try {
            deviceControl.MainPowerOff(85);
            deviceControl.DeviceClose();
        } catch (IOException e) {
            e.printStackTrace();
        }
        super.onDestroy();
    }
}
